package dts.com.mx;

import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import dts.com.mx.vo.User;

/**
 * Handles requests for the application home page.
 */
@Controller
public class HomeController {

	private static final Logger logger = LoggerFactory.getLogger(HomeController.class);

	/**
	 *  Simply selects the home view to render .
	 */

	@RequestMapping(value = "/", method = RequestMethod.GET)
	public String home(Locale locale, Model model , HttpServletRequest request, HttpSession session) {
		logger.info("Welcome home! the client locale is "+ locale.toString());

		Date date = new Date();
		DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);

		logger.info("Contexto " +  request.getContextPath());
		String formattedDate = dateFormat.format(date);

		model.addAttribute("serverTime", formattedDate );

		List<User> userList;

		userList = new ArrayList<User>(20);
		userList.add(new User("Gaurav Arora", "gaurav@myemail.com"));
		userList.add(new User( "Bernard Shaw", "bernard@myemail.com"));
		userList.add(new User( "Aurora Aurealis", "aurora@myemail.com"));
		userList.add(new User( "James Lock", "james@myemail.com" ));
		userList.add(new User( "Karl love", "karl@myemail.com"));
		userList.add(new User( "George jungle", "george@myemail.com"));
		userList.add(new User( "Switch blade", "switch@myemail.com"));
		userList.add(new User( "Gold flake", "gold@myemail.com"));
		userList.add(new User( "Susan Anthony", "susan@myemail.com"));
		userList.add(new User( "Arm Guard", "armg@myemail.com"));
		userList.add(new User( "Raju Gentleman", "raju@myemail.com"));
		userList.add(new User( "Rajat Ghandhi", "rajat@myemail.com"));
		userList.add(new User( "Niketa Shyam", "niketa@myemail.com"));
		userList.add(new User( "G. Mahesh", "mahesh@myemail.com"));
		userList.add(new User("Yogvinder Singh", "yogi@myemail.com"));
		userList.add(new User("Eklavya", "eklavya@myemail.com"));
		userList.add(new User("Asus shyam", "asus@myemail.com"));
		userList.add(new User("Jack donneley", "jack@myemail.com"));
		userList.add(new User( "Donald McDonald", "donald@myemail.com"));
		userList.add(new User("Azure Aloha", "azure@myemail.com"));

		model.addAttribute("userList", userList); 




		return "jsp/home2";
	}

	@RequestMapping(value = "/welcome**" , method = RequestMethod.GET)
	public ModelAndView welcomePage() {

		ModelAndView model = new ModelAndView();
		model.addObject("title", "Spring Security Custom Login Form");
		model.addObject("message", "This is welcome page!");
		model.setViewName("hello");
		return model;

	}

	@RequestMapping(value = "/admin**", method = RequestMethod.GET)
	public ModelAndView adminPage() {

		ModelAndView model = new ModelAndView();
		model.addObject("title", "Spring Security Custom Login Form");
		model.addObject("message", "This is protected page!");
		model.setViewName("admin");

		return model;

	}

	//Spring Security see this :
	@RequestMapping(value = "/login.action", method = RequestMethod.GET)
	public ModelAndView login(
			@RequestParam(value = "error", required = false) String error,
			@RequestParam(value = "logout", required = false) String logout ,Model model ) {

		model.addAttribute("greeting", new User());
		ModelAndView model2 = new ModelAndView();
		if (error != null) {
			model2.addObject("error", "Invalid username and password!");
		}

		if (logout != null) {
			model2.addObject("msg", "You've been logged out successfully.");
		}
		model2.setViewName("login");

		return model2;

	}
	
	@RequestMapping(value = "/profile", method = RequestMethod.GET)
	public @ResponseBody String processAJAXRequest(
			@RequestParam("firstname") String firstname,
			@RequestParam("lastname") String lastname	) {
		String response = "";
		// Process the request
		// Prepare the response string
		return response;
	}
}
